using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using DAL;
using Utility;
using BUS;
using System.IO;
public partial class Admin_SanPhamAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            load_data();
            load_DateTime();
        }

    }
    private void load_data()
    {   // load loai san pham
        ddl_LoaiSP.DataSource = tbLoaiSPBUS.GetAll();
        ddl_LoaiSP.DataTextField = tbLoaiSPColumns.TenLoai.ToString();
        ddl_LoaiSP.DataValueField = tbLoaiSPColumns.IdLoaiSP.ToString();
        ddl_LoaiSP.DataBind();
        //load dia diem
        ddl_DiaDiem.DataSource = tbDiaDiemBUS.GetAll();
        ddl_DiaDiem.DataTextField = tbDiaDiemColumns .TenDiaDiem .ToString();
        ddl_DiaDiem.DataValueField = tbDiaDiemColumns.IdDiaDiem.ToString();
        ddl_DiaDiem.DataBind();
        //load trang thai san pham
        ddl_TrangThaiSP.DataSource = tbTrangThaiSPBUS.GetAll();
        ddl_TrangThaiSP.DataTextField = tbTrangThaiSPColumns.TenTrangThai.ToString ();
        ddl_TrangThaiSP.DataValueField = tbTrangThaiSPColumns.IdTrangThaiSP.ToString ();
        ddl_TrangThaiSP.DataBind();
        //load nhan vien
        ddl_NhanVien.DataSource = tbNhanVienBUS.GetAll();
        ddl_NhanVien.DataTextField = tbNhanVienColumns.TenNhanVien.ToString();
        ddl_NhanVien.DataValueField = tbNhanVienColumns.IdNhanVien.ToString();
        ddl_NhanVien.DataBind();
        //load doanh nghiep
        ddl_DoanhNghiep.DataSource = tbDoanhNghiepBUS.GetAll();
        ddl_DoanhNghiep.DataTextField = tbDoanhNghiepColumns.TenDoanhNghiep.ToString();
        ddl_DoanhNghiep.DataValueField = tbDoanhNghiepColumns.IdDoanhNghiep.ToString();
        ddl_DoanhNghiep.DataBind();
    }
    private void load_DateTime()
    {
        for (int i = 1; i <= 31; i++)
        {
            ddl_ngaybdban .Items.Add(i.ToString ("00"));
            ddl_ngayktban .Items.Add(i.ToString("00"));
            ddl_ngaycn.Items.Add(i.ToString("00"));
            ddl_ngaydnban.Items.Add(i.ToString("00"));  
        }
        for (int i = 1; i <= 12; i++)
        {
            ddl_thangbdban.Items.Add(i.ToString("00"));
            ddl_thangktban .Items.Add(i.ToString("00"));
            ddl_thangcn .Items.Add(i.ToString("00"));
            ddl_thangdnban .Items.Add(i.ToString("00"));
        }
        for(int i=DateTime .Now .Year -10;i<=DateTime .Now .Year +10;i++)
        {
            ddl_nambdban.Items.Add(i.ToString());
            ddl_namktban.Items.Add(i.ToString());
            ddl_namcn .Items .Add (i.ToString ());
            ddl_namdnban .Items .Add (i.ToString ());
        }
    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        tbSanPham sanPham = new tbSanPham();
        sanPham.TenSP = txt_TenSP.Text;
        sanPham.IdLoaiSP = int.Parse(ddl_LoaiSP.SelectedValue);
        sanPham .IdDiaDiem =int.Parse (ddl_DiaDiem .SelectedValue );
        sanPham.IdTrangThaiSP = int.Parse(ddl_TrangThaiSP.SelectedValue);
        sanPham.IdNhanVien = int.Parse(ddl_NhanVien.SelectedValue);
        sanPham.IdDoanhNghiep = int.Parse(ddl_NhanVien .SelectedValue);
        sanPham.GiaBan = int.Parse (txt_GiaBan.Text);
        sanPham.GiaTriThuc = int.Parse (txt_GiaTriThuc.Text);
        sanPham.DKSungDung = txt_dksd.Text;
        sanPham.MoTa = txt_Mota.Text;
        sanPham.SLBan = int.Parse (txt_slban.Text);
        sanPham.SLDaBan = int.Parse (txt_slDaBan.Text);
        sanPham.NgayBDBan = DateTime.Parse(ddl_thangbdban.SelectedValue + "/" + ddl_ngaybdban.SelectedValue + "/" + ddl_nambdban.SelectedValue);
        sanPham.NgayKTBan = DateTime.Parse(ddl_thangktban.SelectedValue + "/" +  ddl_ngayktban.SelectedValue + "/" + ddl_namktban.SelectedValue);
        sanPham.NgayUpdate = DateTime.Parse(ddl_thangcn.SelectedValue + "/" + ddl_ngaycn.SelectedValue + "/" + ddl_namcn.SelectedValue);
        sanPham.NgayDNBan = DateTime .Parse (ddl_thangdnban .SelectedValue + "/" + ddl_ngaydnban.SelectedValue + "/"  + ddl_namdnban.SelectedValue);
        sanPham.TLHoaHong = float .Parse (txt_tlhoahong .Text);
        int iD= tbSanPhamBUS.Insert_SP(sanPham);
        HttpFileCollection hfc = Request.Files;
        for (int i = 0; i < hfc.Count; i++)
        {
            HttpPostedFile hpf = hfc[i];
            if (hpf.ContentLength > 0)
            {
                string filename = DateTime.Now.Ticks.ToString() + "_" + Path.GetFileName(hpf.FileName);
                hpf.SaveAs(Server.MapPath("~/Photos/") + "\\" + filename .ToString ());

                System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~/Photos/") + filename);
                ImageUtility.MakeThumbnailPhoto(img, 100, 100, Server.MapPath("~/Photos/Thumb/"), filename);
                tbHinhAnh hinhAnh = new tbHinhAnh();
                hinhAnh.TenHinh = filename.ToString();
                hinhAnh.DuongDan = "Photos/" + filename.ToString();
                hinhAnh.IdSanPham = iD;
                tbHinhAnhBUS.Insert(hinhAnh);
            }
        }
        Response.Redirect("~/Admin/SanPhamList.aspx");
    }
}
